<template>
  <div class="outSource">
    <jc-button
      :btn-arr="['reload', 'remove', 'sub', 'option', 'batchFill', !list.length && 'check']"
      :disab-arr="['close', 'cancel', 'using', 'freeze']"
      :fbillno="fbillno"
      url="tSubPickmtrl/querySubPickmtrlList"
      @refresh="handleGetUntreated"
      @submit="submit(1)"
      @noSubmit="submit(2)"
      @audit="audit(1)"
      @noAudit="audit(2)"
      @toExcel="handleExport"
      @del="handleDelete"
      @handleCom1="handleCom1('1')"
      @handleCom2="handleCom1('2')"
      @check="check('up')"
      @check1="check('down')"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQuerySonClass"
        />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleQuerySonClass"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        sortable
        table-selection
        get-row-keys="fentryId"
        :get-summaries="getSummaries"
        show-summary
        @selectionChange="handleSelectAll"
        @clickRow="proOrder"
      >
        <!--        编号-->
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="proOrder(col.scope.row)">
            {{ col.scope.row.fbillNo }}
          </el-link>
        </template>
        <!--    物料编码    -->
        <template v-slot:orderSlot="col">
          <el-link type="primary" @click="jumpMateriel(col.scope.row)">
            {{ col.scope.row.fnumber }}
          </el-link>
        </template>
        <!--        审核状态-->
        <template v-slot:btnState="col">
          <span v-if="col.scope.row.fdocumentstatus === ''" />
          <el-tag v-else :type="col.scope.row.stateColor">
            {{ col.scope.row.fdocumentstatus }}
          </el-tag>
        </template>
      </jc-table>
      <cardTab @search="handleQueryBomList" />
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
    <upDown v-if="upDownVis" :msg="cha" :fentryid="fentryData" @client="upDownVis=false" />
  </div>
</template>

<script>
import {
  querySubPickmtrlList, notsubmitSubPickmtrl, submitSubPickmtrl,
  unauditSubPickmtrls, auditSubPickmtrls, delSubPickmtrls
} from '@/api/production/productionPicking'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'
import { toMxBmina } from '@/components/ToMxamineState'

export default {
  name: 'ProductionPicking',
  components: {
    cardTab: () => import('@/components/cardTab'),
    upDown: () => import('@/components/upDown'),
    search,
    jcButton,
    filtration
  },
  mixins: [searData],
  props: {
    list: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      upDownVis: false,
      cha: '',
      fentryData: [],
      ftype: 69,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      Fisdefault: '',
      // 表头
      tableData: [], // 生产领料数据
      tableHeader: [
        { label: this.$t('market.billNo'), type: 'tag', prop: 'fbillNo', width: '120px' },
        { label: this.$t('market.orderDate'), prop: 'fdate', width: '90' },
        { label: this.$t('market.documentstatus'), type: 'state', prop: 'fdocumentstatus', width: '100px' },
        { label: this.$t('market.paezText1'), prop: 'salKhddh', width: '110' },
        { label: this.$t('market.supplier'), prop: 'fsupplierName' },
        { label: this.$t('market.productmodel'), prop: 'fproductmodel', width: '110' },
        { label: this.$t('market.fmaterial'), prop: 'fnumber', type: 'order', width: '110px' },
        { label: this.$t('shop.fname2'), prop: 'fname', minWidth: '130px' },
        { label: this.$t('market.specify'), prop: 'fspecificaTion' },
        { label: this.$t('market.funit'), prop: 'funitName', width: '80' },
        { label: this.$t('market.shouldNum'), prop: 'fmustqty', align: 'right', width: '80' },
        { label: this.$t('market.realqty'), prop: 'factualQty', align: 'right', width: '80' },
        { label: this.$t('market.stockQty'), prop: 'fbaseQty', align: 'right', width: '80' },
        { label: this.$t('market.stock2'), prop: 'fstockName', width: '120' },
        { label: this.$t('market.fmodel2'), prop: 'fmodel' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescripTion', minWidth: '200px' },
        { label: this.$t('market.mobillNo'), prop: 'fmobillNo', width: '110' },
        { label: this.$t('market.pbomBillNo'), prop: 'ppbomBillNo', width: '120' },
        { label: this.$t('market.creator'), prop: 'fcreator' },
        { label: this.$t('market.createDate'), prop: 'fcreatedate', width: '110' }
      ],
      fbillno: '',
      tableHeader1: [
        { label: this.$t('market.branches'), prop: 'fbranches', width: '80' }
      ]
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader.concat(this.tableHeader1)
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.val && this.val.length > 0) {
          const list = this.val
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '委外领料列表')
        } else {
          querySubPickmtrlList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list.map(item => {
                return (toMxBmina(item))
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '委外领料列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取委外列表数据
    handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        list: this.list,
        pageNum: this.currentPage,
        pageSize: this.size,
        search: this.searCollData.concat(this.details)
      }
      querySubPickmtrlList(DATA).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.list.map(item => {
            return (toMxBmina(item))
          })
          this.total = res.data.total
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 成品 原材料
    handleQueryBomList(ev) {
      this.searCollData = []
      this.currentPage = 1
      this.searCollData = ev
      this.searchSon = ev
      this.handleGetUntreated()
    },
    // 详情
    proOrder(ev) {
      const id = ev.fid
      this.$router.push({
        path: `/outOrder/${id}`
      })
    },
    // 套打
    handleCom1(ev) {
      const id = this.selectAll.join(',')
      if (ev === '1') {
        this.$router.push({
          path: `/outsouTong1/${id}`
        })
      } else {
        this.$router.push({
          path: `/outsouLian1/${id}`
        })
      }
    },
    // 全选
    handleSelectAll(item) {
      this.val = item
      this.fbillno = item[0].fbillNo
      this.selectAll = []
      this.fentryData = []
      item.forEach(res => {
        this.fentryData.push(res.fentryId)
        res.fbillNo && this.selectAll.push(res.fbillNo)
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: this.selectAll }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.handleGetUntreated()
        this.selectAll = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    submit(ev) {
      const api = ev === 1 ? submitSubPickmtrl : notsubmitSubPickmtrl
      this.toExamineFun(api)
    },
    // 审核
    audit(ev) {
      const api = ev === 1 ? auditSubPickmtrls : unauditSubPickmtrls
      this.toExamineFun(api)
    },
    // 删除
    handleDelete() {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const api = delSubPickmtrls
          this.toExamineFun(api)
        })
    },
    // 上查、下查
    check(ev) {
      if (this.fentryData.length === 0) return
      this.cha = ev
      this.upDownVis = true
    }
  }
}
</script>
<style lang="scss" scoped>
.outSource {
  @include listBom;

  .header{
    position:relative;
    .header-name{
      width: 100%;
      .btn {
        transform: translateY(18%);
        margin-left: 450px !important;
      }
    }
  }
}
</style>
